//app.js
var util = require('./utils/util.js');
var api;
App({
    is_on_launch: true,
    onLaunch: function () {
      this.setApi();
      api = this.api;

      // this.getNavigationBarColor();
      // console.log(wx.getSystemInfoSync());
      this.getStoreData();
    },
    getStoreData: function () {
      var page = this;
      this.request({
        url: api.default.home,
        success: function (res) {
          // console.log(res)
          if (res != null) {
            wx.setStorageSync("banner", res.banner);
            wx.setStorageSync("basic", res.basic);
            wx.setStorageSync("honor", res.honor);
            wx.setStorageSync("news", res.news);
            wx.setStorageSync("pro_list", res.pro_list);
            wx.setStorageSync("product", res.product);
            wx.setStorageSync("youshi", res.youshi);
          }
        },
        complete: function () {
          // page.login();
        }
      });
    },

    request: function (object) {
      if (!object.data)
        object.data = {};
      var access_token = wx.getStorageSync("access_token");
      if (access_token) {
        object.data.access_token = access_token;
      }
      object.data._uniacid = this.siteInfo.uniacid;
      object.data._acid = this.siteInfo.acid;
      wx.request({
        url: object.url,
        header: object.header || {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: object.data || {},
        method: object.method || "GET",
        dataType: object.dataType || "json",
        success: function (res) {
          if (res.data.code == -1) {
            //getApp().login();
          } else {
            if (object.success)
              object.success(res.data);
          }
        },
        fail: function (res) {
          var app = getApp();
          if (app.is_on_launch) {
            app.is_on_launch = false;
            wx.showModal({
              title: "网络请求出错",
              content: res.errMsg,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  if (object.fail)
                    object.fail(res);
                }
              }
            });
          } else {
            wx.showToast({
              title: res.errMsg,
              image: "/images/icon-warning.png",
            });
            if (object.fail)
              object.fail(res);
          }
        },
        complete: function (res) {
          if (object.complete)
            object.complete(res);
        }
      });
    },
    saveFormId: function (form_id) {
      this.request({
        url: api.user.save_form_id,
        data: {
          form_id: form_id,
        }
      });
    },

    api: require('api.js'),
    setApi: function () {
      var siteroot = this.siteInfo.siteroot;
      siteroot = siteroot.replace('app/index.php', '');
      siteroot += 'addons/zjhj_mall/core/web/index.php?store_id=-1&r=api/';

      function getNewApiUri(api) {
        for (var i in api) {
          if (typeof api[i] === 'string') {
            api[i] = api[i].replace('{$_api_root}', siteroot);
          } else {
            api[i] = getNewApiUri(api[i]);
          }
        }
        return api;
      }

      this.api = getNewApiUri(this.api);
    },
    siteInfo: {
      'uniacid': '-1',
      'acid': '-1',
      'version': '1.0.0',
      'siteroot': 'http://luwei-pc.tunnel.qydev.com/php/we7offical/app/index.php',
    },
    pageOnLoad: function (page) {
      console.log('--------pageOnLoad----------');
      this.setNavigationBarColor();
      this.setPageNavbar(page);
    },
    pageOnReady: function (page) {
      console.log('--------pageOnReady----------');

    },
    pageOnShow: function (page) {
      console.log('--------pageOnShow----------');
    },

    setPageNavbar: function (page) {
      console.log('----setPageNavbar----');
      console.log(page);
      var navbar = wx.getStorageSync('_navbar');

      if (navbar) {
        setNavbar(navbar);
      }
      this.request({
        url: api.default.navbar,
        success: function (res) {
          if (res.code == 0) {
            setNavbar(res.data);
            wx.setStorageSync('_navbar', res.data);
          }
        }
      });

      function setNavbar(navbar) {
        var in_navs = false;
        var route = page.route || (page.__route__ || null);
        for (var i in navbar.navs) {
          if (navbar.navs[i].url === "/" + route) {
            navbar.navs[i].active = true;
            in_navs = true;
          } else {
            navbar.navs[i].active = false;
          }
        }
        if (!in_navs)
          return;
        page.setData({ _navbar: navbar });
      }

    },

    getNavigationBarColor: function () {
      var app = this;
      app.request({
        url: api.default.navigation_bar_color,
        success: function (res) {
          if (res.code == 0) {
            wx.setStorageSync('_navigation_bar_color', res.data);
            app.setNavigationBarColor();
          }
        }
      });
    },

    setNavigationBarColor: function () {
      var navigation_bar_color = wx.getStorageSync('_navigation_bar_color');
      if (navigation_bar_color) {
        wx.setNavigationBarColor(navigation_bar_color);
      }
    },
})